package com.mv.javamvc.controller;

import com.mv.javamvc.pojo.User;
import com.mv.javamvc.service.UserService;
import com.mv.javamvc.service.impl.UserServiceImpl;
import com.mv.javamvc.view.View;
import com.mv.webserver.http.HttpRequest;
import com.mv.webserver.http.HttpResponse;
import com.mv.webserver.http.HttpServlet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.thymeleaf.context.Context;

import java.io.UnsupportedEncodingException;
import java.util.List;



public class UserListServlet extends HttpServlet {
    private static final Logger log = LoggerFactory.getLogger(UserListServlet.class);
    
    private UserService userService = new UserServiceImpl(); 


	@Override
	public void doGet(HttpRequest request, HttpResponse response) {
		// TODO Auto-generated method stub
	
	       log.info("UserListServlet start..");
	       List<User> list =  userService.list();
	
	       Context context = new Context();
	       context.setVariable("users",list);
	       String html = View.show("webapps/myweb/userList.html",context);
	        try {
	            response.setData(html.getBytes("UTF-8"));
	            response.putHeader("Content-Length","text/html");
	        } catch (UnsupportedEncodingException e) {
	            log.error(e.getMessage(),e);
	        }
	        log.info("UserListServlet end");		
		
	}


	
	
}
